package com.summer.campussct.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

// 如果是LocalDateTime的话有LocalDateTimeDeserializer和LocalDateTimeSerializer
@Configuration
public class LocalDateSerializationConfig {

    /**
     * 序列化内容
     *   LocalDate -> String
     * 服务端返回给客户端内容
     * */
    @Bean
    public LocalDateSerializer localDateTimeSerializer() {
        return new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 反序列化内容
     *   String -> LocalDate
     * 客户端传入服务端数据
     * */
    @Bean
    public LocalDateDeserializer localDateTimeDeserializer() {
        return new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    // 配置
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
            builder.deserializerByType(LocalDateTime.class, localDateTimeDeserializer());
        };
    }
}